import React from 'react';
import { Layout } from 'antd';
import { UserOutlined } from '@ant-design/icons'
import { connect } from 'dva';
import router from 'umi/router';
import SiderMenu from '@/components/SiderMenu';
import { getFlatMenuKeys } from '@/components/SiderMenu/SiderMenuUtils';
import memoizeOne from 'memoize-one';
import isEqual from 'lodash/isEqual';
import styles from './BasicLayout.less';
import * as requesturl from "@/services/request-url";
/*eslint-disable*/

const {  Content } = Layout;

function getMenuItem(route) {
  if (route.name) {
    const menuItem = {
      name: route.name,
      path: route.path,
      icon: route.icon,
      hideInMenu: route.hideInMenu ? route.hideInMenu : false
    };
    if (Array.isArray(route.routes) && route.routes.length > 0 && !route.hideChildrenInMenu) {
      const children = [];
      route.routes.forEach((item) => {
        const child = getMenuItem(item);
        if (child) {
          children.push(child);
        }
      });
      if (children.length > 0) {
        menuItem.children = children;
      }
    }
    return menuItem;
  }
  return null;
}

function getMenu(routes) {
  const menu = [];
  routes.forEach((route) => {
    const menuItem = getMenuItem(route);
    if (menuItem) {
      menu.push(menuItem);
    }
  });
  return menu;
}

const memoizeOneGetMenu = memoizeOne(getMenu, isEqual);

@connect()

class BasicLayout extends React.PureComponent {

  handleMenuClick = ({ key }) => {
    // const { dispatch } = this.props;
    if (key === 'userCenter') {
      router.push('/account/center');
      return;
    }
    if (key === 'userinfo') {
      router.push('/account/settings');
      return;
    }
  };
  //tokenn
  async getToken() {
    let token = undefined;
    const casTokenUrl = `${requesturl.login}/cas/jwt/token.do`;
    await fetch(casTokenUrl, {
      method: 'POST',
      credentials: 'include',
    }).then(res =>
      res.json().then(data => {
        const { returnStatus, returnInfo } = data
        if (returnStatus == 200) {
          const currentTime = new Date().getTime();
          window.tokenLastUpdateTime = currentTime;
          window.epmToken = returnInfo;
          token = returnInfo;
          sessionStorage.setItem("token",token)
          this.getUserInfo()
        } else if (returnStatus == 401){ // 未登录
          // window.location = `http://172.16.193.25:9009` + returnInfo;
          let a = document.createElement("a")
          a.href = `${requesturl.login}/cas/jwt/login.do`;
          a.click();
        }
      })
    );
  }
  componentDidMount=()=>{
    this.getToken()
  }
  //拿user信息
  getUserInfo = () => {
    this.props
      .dispatch({
        type: "global/getUserInfo",
        payload: {}
      })
      .then(res => {
        console.log(res);
        if (res) {
          this.setState({
            username: res.data.userName
          })
          sessionStorage.setItem("userId", res.data.userId)
        }
      });
  }
  render() {
    const { route: { routes }, children } = this.props;
    const menuData = memoizeOneGetMenu(routes);
    const flatMenuKeys = getFlatMenuKeys(menuData);

    // const menu = (
    //   <Menu>
    //     <Menu.Item key="0">
    //       <a href="https://www.antgroup.com">1st menu item</a>
    //     </Menu.Item>
    //     <Menu.Item key="1">
    //       <a href="https://www.aliyun.com">2nd menu item</a>
    //     </Menu.Item>
    //     <Menu.Divider />
    //     <Menu.Item key="3">user</Menu.Item>
    //   </Menu>
    // );
    return (
      <Layout style={ { minHeight: '100vh' } }>
        {/* <SiderMenu
          onCollapse="0"
          menuData={ menuData }
          flatMenuKeys={ flatMenuKeys }
          { ...this.props }
        /> */}
        <Layout>
          {/* <div className={ styles.head }>
              <span style={ {float: "right",margin:"10px 20px"} } >
                <span style={ { margin: "10px" } }><a href={`${requesturl.login}/cas/jwt/login.do`} >登录</a></span>
                <UserOutlined style={ { fontSize: '20px', height: '100%', color:"white" } } title="用户名"/>
              </span>
          </div>  */}
          <Content style={ { margin: '24px' } }>
            <div style={ { minHeight: 'calc(100vh - 112px)', overflow: 'hidden' } }>
              {
                children.props.location.pathname==="/"? <div className={styles.welcome}>欢迎进入军医科后台管理</div>:children
              }
            </div>
          </Content>
        </Layout>
      </Layout>
    );
  }
}

export default BasicLayout;